#include "head.h"

int sign_up(int net_fd, MYSQL* mysql){
    fprintf(stdout,"%s\n","在注册");
    //初始化数据库

    int type = 0;
    int ret_recv, ret_sqlquery;
    char str_mysql[4096] = {0};
    char userName[512] = {0};
//printf("111\n");
    bool flag;
    while(1){
        memset(userName, 0, sizeof(userName));

        off_t username_len;
        ret_recv = recv(net_fd, &username_len, sizeof(off_t), MSG_WAITALL);
        if(ret_recv == -1){
            printf("对方断开连接！\n");
            return 1;
        }


        ret_recv = recv(net_fd, userName, username_len, MSG_WAITALL);
        if(ret_recv == -1){
            printf("对方断开连接！\n");
            return 1;
        }
//printf("%ld: %s\n", username_len, userName);
        snprintf(str_mysql, sizeof(str_mysql), "select * from user where `user_name` = '%s'", userName);
//printf("222\n");
        mysql_set_character_set(mysql, "utf8mb4");
//printf("%s\n", str_mysql);
        int ret_sqlquery = mysql_query(mysql, str_mysql);
        //MYSQL_RES* result = mysql_store_result(mysql);
//printf("ret: %d\n", ret_sqlquery);
        
        MYSQL_RES* res = mysql_store_result(mysql);  
        MYSQL_ROW row;
        while (row = mysql_fetch_row(res)) {  
            ret_sqlquery = 1;
        }
        mysql_free_result(res);

        if (ret_sqlquery == -1) {
            type = -1;
            send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
            //TODO:日志
            
            
        }else if(ret_sqlquery){
//printf("到第二步了\n");
            type = 1;
            send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
            //TODO:日志

//printf("ERROR: %s\n",mysql_error(mysql));
            
        }else if(ret_sqlquery == 0){
//printf("到第三步了\n");
            type = 0;
            send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
            break;
        }
//sleep(1);
    }
//printf("333333\n");

    off_t password_len;
    ret_recv = recv(net_fd, &password_len, sizeof(off_t), MSG_WAITALL);
    if(ret_recv == -1){
        printf("对方断开连接！\n");
        return 1;
    }


    char passWord[64] = {0};
    ret_recv = recv(net_fd, passWord, password_len, MSG_WAITALL);
    if(ret_recv == -1){
        printf("对方断开连接！\n");
        return 1;
    }
//printf("%ld: %s\n", password_len, passWord);
    char salt[13] = "$6$";
    int tmp = 3;
    for(int i=0;i<8;){
        int random_num = rand() % 58 + 65;  // 生成 65 到 122 之间的随机数
        if(random_num>=91&&random_num<=96){
            continue;
        }else{
            i++;
            salt[tmp++] = (char)random_num;
        }
    }
    salt[tmp] = '$';
    salt[tmp+1] = '\0';
//printf("%s\n", salt);
    memset(str_mysql, 0, sizeof(str_mysql));
    
//printf("%ld password: %s\n", strlen(passWord), passWord);
//printf("%ld salt: %s\n", strlen(salt), salt);
    snprintf(str_mysql, sizeof(str_mysql), 
             "insert into user (user_name, password, salt) values ('%s', '%s', '%s')",
             userName, crypt(passWord, salt), salt);
//printf("%s\n", str_mysql);
    ret_sqlquery = mysql_query(mysql, str_mysql);
    if (ret_sqlquery) {
        type = -1;
        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        //TODO:日志

//printf("ERROR: %s\n",mysql_error(mysql));
        return -1;
    }  
        
    type = 0;
    send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
//printf("Successful!\n");
    printf("注册成功！\n");

    memset(str_mysql, 0, sizeof(str_mysql));
    
    char path[514] = {0};
    snprintf(path, sizeof(path), "/%s", userName);

    snprintf(str_mysql, sizeof(str_mysql), 
             "select * from user where `user_name` = '%s'", userName);
    mysql_set_character_set(mysql, "utf8mb4");
    ret_sqlquery = mysql_query(mysql, str_mysql);

    
    MYSQL_RES* res = mysql_store_result(mysql);  
    MYSQL_ROW row;
    char id[12] = {0};
    while(row = mysql_fetch_row(res)){
        strcpy(id, row[0]);
    }

    memset(str_mysql, 0, sizeof(str_mysql));
    snprintf(str_mysql, sizeof(str_mysql), 
             "insert into file (path, user_id, Parent_dir_id, file_type, file_name) values ('%s', '%s', '%s', '%s', '%s')",
             path, id, "-1", "0", userName);
printf("%s\n", str_mysql);
    ret_sqlquery = mysql_query(mysql, str_mysql);
    if (ret_sqlquery) {
        type = -1;
        send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);
        //TODO:日志

        return -1;
    }  



    return 0;
}
//bool sign_in();
int sign_in(int net_fd, MYSQL* mysql){
    
    //先发送token

    off_t username_len = 0;
    off_t password_len = 0;
    char userName[512] = {0};
    char passWord[64] = {0};

    recv(net_fd, &username_len, sizeof(off_t), MSG_WAITALL);
    recv(net_fd, userName, username_len, MSG_WAITALL);
    recv(net_fd, &password_len, sizeof(off_t), MSG_WAITALL);
    recv(net_fd, passWord, password_len, MSG_WAITALL);

printf("%s: %s\n", userName, passWord);



    char str_mysql[1024] = {0};

    int type = 1;
    snprintf(str_mysql, sizeof(str_mysql), 
             "select * from user where `user_name` = '%s'", userName);
//printf("222\n");
    mysql_set_character_set(mysql, "utf8mb4");
//printf("%s\n", str_mysql);
    int ret_sqlquery = mysql_query(mysql, str_mysql);

//printf("ret: %d\n", ret_sqlquery);
    
    MYSQL_RES* res = mysql_store_result(mysql);  
    MYSQL_ROW row;
    char id[12] = {0};
    while(row = mysql_fetch_row(res)){
//printf("%ld password: %s\n", strlen(passWord), passWord);
//printf("%ld salt: %s\n", strlen(row[3]), row[3]);
        char* tmp = crypt(passWord, row[3]);
        type = (strcmp(tmp, row[2])==0)? 0 : 1;
        strcpy(id, row[0]);
//printf("存储的hash：%s\n", row[2]);
//printf("生成的hash：%s\n", tmp);
    }
//printf("type: %d\n", type);
    send(net_fd, &type, sizeof(int), MSG_NOSIGNAL);

    if(type == 0){
        off_t id_len = strlen(id);
//printf("%ld: %s\n", id_len, id);
        send(net_fd, &id_len, sizeof(off_t), MSG_NOSIGNAL);
        send(net_fd, id, id_len, MSG_NOSIGNAL);


        char token[1024] = {0};
        memset(token, 0, sizeof(token));
//printf("is here!\n");
        make_JWT(token, net_fd, id);
//printf("token: %s\n", token);
        off_t token_len = strlen(token);
        send(net_fd, &token_len, sizeof(off_t), MSG_NOSIGNAL);
        send(net_fd, token, token_len, MSG_NOSIGNAL);
//printf("%ld: %s\n", token_len, token);
        
    }



    return 0;

}
